package com.steel.yfmall.cloud.assistant.utils.zg;

import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgCallWsdlAPIBaseDTO;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;

/**
 * 中冠接口处理
 */
@Slf4j
@Service
public class ZgCallServiceUtils{


    public APIMsgBean<String> commonCallZgSoApi(Object object, ZgCallWsdlAPIBaseDTO zgCallWsdlAPIBaseDTO) {
        APIMsgBean<String> stringAPIMsgBean = new APIMsgBean<>();
        try {
            if (ObjectUtils.isEmpty(object)) {
                stringAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("请求中冠系统API请求参数信息为空"));
                return stringAPIMsgBean;
            }
            if (ObjectUtils.isEmpty(zgCallWsdlAPIBaseDTO)) {
                stringAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("请求中冠系统API请求信息为空"));
                return stringAPIMsgBean;
            }
            if (StringUtils.isBlank(zgCallWsdlAPIBaseDTO.getUrl()) || StringUtils.isBlank(zgCallWsdlAPIBaseDTO.getInterfaceName()) || StringUtils.isBlank(zgCallWsdlAPIBaseDTO.getParameterType())) {
                stringAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("请求中冠系统API请求信息Url或者方法名为空"));
                return stringAPIMsgBean;
            }
            org.apache.axis.client.Service service = new org.apache.axis.client.Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(zgCallWsdlAPIBaseDTO.getUrl()));
            //WSDL里面描述的接口名称
            call.setOperationName(new QName(null, zgCallWsdlAPIBaseDTO.getInterfaceName()));
            //接收的参数名称
            call.addParameter(new QName(zgCallWsdlAPIBaseDTO.getParameterType()), XMLType.XSD_DATE, ParameterMode.IN);
            //设置返回类型
            call.setReturnType(XMLType.XSD_STRING);
            call.setUseSOAPAction(true);
            String jsonStr = JSONObject.toJSONString(object);
            //记录日志
            log.info("获取中冠接口调用参数:" + jsonStr);
            // 调用方法
            String result = (String) call.invoke(new Object[]{jsonStr});
            if (StringUtils.isBlank(result)) {
                stringAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("请求中冠系统API请求返回为空,请求参数:" + jsonStr));
                return stringAPIMsgBean;
            }
            //输出结果集
            log.info("调用中冠接口返回参数:" + result);
            stringAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
            stringAPIMsgBean.setData(result);
        } catch (Exception e) {
            stringAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("请求中冠系统API请求处理异常"));
            log.error("发送中冠接口请求失败,参数为:" + JSONObject.toJSONString(object), e);
        }
        return stringAPIMsgBean;
    }

}
